package impl;

import bean.OrderInfo;
import dao.OrderInfoDao;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @classname:OrderInfoDaoImpl
 * @description:
 * @author: Mr.Z
 * @date: 2021/1/2 10:35
 * @version: 0.1
 * @since:1.8
 */
public class OrderInfoDaoImpl implements OrderInfoDao {
    private Connection conn;
    private String sql;
    private PreparedStatement ps;
    private ResultSet rs;
    private int result;

    public OrderInfoDaoImpl(Connection conn) {
        this.conn = conn;
    }
    public OrderInfoDaoImpl() {
    }

    @Override
    public int addOrderInfo(OrderInfo orderInfo) {
        sql = "insert into orderinfo(mid,total,paytype) values (?,?,?)";
        try {
            ps =conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1,orderInfo.getMid());
            ps.setObject(2,orderInfo.getTotal());
            ps.setInt(3,orderInfo.getPaytype());
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            if(rs.next()){
                result = (int) rs.getLong(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int addOrderAndDetail(int uid, Map<Integer, Integer> map) {
        StringBuilder builder = new StringBuilder();
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, Integer> next = iterator.next();
            builder.append("(");
            builder.append(uid);
            builder.append(",");
            builder.append(next.getKey());
            builder.append(",");
            builder.append(next.getValue());
            builder.append(")");
            builder.append(",");
        }
        builder.deleteCharAt(builder.lastIndexOf(","));
        sql = "insert into detail(uid,id,count) values "+builder;
//        System.out.println(sql);
        try {
            ps = conn.prepareStatement(sql);
           result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateGoodNumberById(int id, int number) {
//        conn= DBHelper.getCon();
        sql = "update goodinfo set number=number-? where id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,number);
            ps.setInt(2,id);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateMemberBalanceById(BigDecimal money, int mid) {
        sql="update member set balance=balance+? where mid = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1,money);
            ps.setInt(2,mid);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}
